package com.wfit.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wfit.admin.common.Constants;
import com.wfit.admin.common.Result;
import com.wfit.admin.entity.Dict;
import com.wfit.admin.entity.Menu;
import com.wfit.admin.mapper.DictMapper;
import com.wfit.admin.service.IMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马真志
 * @since 2023-12-13
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

        @Resource
        private IMenuService menuService;
        @Resource
        private DictMapper dictMapper;
        //
        @GetMapping("/icons")
        public Result geyIcon() {
                QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
                dictQueryWrapper.eq("type", Constants.DICT_TYPE);
                return Result.success(dictMapper.selectList(null));
        }
        @PostMapping
        public Boolean save(@RequestBody Menu menu) {
                return menuService.saveOrUpdate(menu);
                }

        @DeleteMapping("/{id}")
        public Boolean delete(@PathVariable Integer id) {
                return menuService.removeById(id);
                }

        @GetMapping
        public Result findAll(@RequestParam(defaultValue = "") String name) {
                return Result.success(menuService.findMenus(name));
        }

        @GetMapping("/{id}")
        public Menu findOne(@PathVariable Integer id) {
        return menuService.getById(id);
        }
        @PostMapping("/batch")
        public Boolean deleteBatch(@RequestBody List<Integer> ids) {
                return menuService.removeByIds(ids);
        }
        //获取菜单id
        @GetMapping("/ids")
        public Result findAllIds() {
                return Result.success(menuService.list().stream().map(Menu::getId));
        }

}

